import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'

import { useMount } from 'ahooks'

import PropTypes from 'prop-types'
import zhCN from 'antd/locale/zh_CN'
import { ConfigProvider, App } from 'antd'
import { lazy, Suspense, useState } from 'react'
import { BrowserRouter, useRoutes } from 'react-router-dom'

import Localhost from '../Log/LogBox/localhost'

import { IndexedDBFun } from '../Utils/IndexedDB'
import { judgeBrowserVersion } from '../Utils/Judgment'
import { debuggerFun } from '../Utils/configFun'


const { hostname, port } = window.location

dayjs.locale('zh-cn')

const VersionTips = lazy(() => import('../View/TipsPage/TipsVersion'))
const Loading = lazy(() => import('../View/Loading/LoadSvg'))

const Log = lazy(() => import('../Log'))
const Admin = lazy(() => import('../Admin'))
const Start = lazy(() => import(`../Start`))


const ProjectRouter = [
    { path: '/', element: <Log /> },
    { path: 'Admin', element: <Admin /> },
    { path: 'Start/:page/:id?', element: <Start /> }
]

const Router = () => {
    const [Router, setRouter] = useState()
    const [EdiTionState, setEdiTionState] = useState(true)

    const Routes = ({ data }) => {
        Routes.propTypes = { data: PropTypes.object }
        return useRoutes(data)
    }

    useMount(() => {
        if (hostname !== 'localhost') {
            const BrowserVersion = judgeBrowserVersion()
            setEdiTionState(BrowserVersion)
            if (BrowserVersion) {
                window.webDB = IndexedDBFun()
                setRouter(ProjectRouter)
            }
        }
    })

    if (!port) {
        debuggerFun()
    }

    if (hostname === 'localhost') return <Localhost />
    else return (
        <ConfigProvider locale={zhCN}>
            <App>
                {Router ?
                    <BrowserRouter>
                        <Suspense fallback={<Loading />} >
                            {EdiTionState ? <Routes data={Router} /> : <VersionTips />}
                        </Suspense>
                    </BrowserRouter> : <Loading />
                }
            </App>
        </ConfigProvider>
    )
};

export default Router;